#include "Window.h"
#include "stm32f10x.h"
WM_HWIN hRep;
extern USART cout;
#define ID_LISTBOX_REPLY (GUI_ID_USER+0)
#define ID_BUTTON_EXIT (GUI_ID_USER+1)
static void _cbRep(WM_MESSAGE* pMsg)
{
	static LISTBOX_Handle hList;
	switch(pMsg->MsgId)
	{
		case WM_CREATE:
			hList = LISTBOX_CreateEx(30, 20, 260, 220, pMsg->hWin, WM_CF_SHOW, 0, ID_LISTBOX_REPLY, nullptr);
			LISTBOX_SetFont(hList,&UTF_8);
		//LISTBOX_SetItemSpacing()
			res = f_opendir(&dir, "/Reply");
			if(res != FR_OK)
				cout << "diropen fail" << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
			while(true)
			{
				res = f_readdir(&dir, &info);
				if(res or info.fname[0] == 0)
					break;
				LISTBOX_AddString(hList, info.fname);
			}
			f_closedir(&dir);

//			LISTBOX_AddString(hList, "000.txt");
//			LISTBOX_AddString(hList, "001.txt");
//			LISTBOX_AddString(hList, "002.txt");
			BUTTON_SetText(BUTTON_CreateEx(5, 110, 20, 20, pMsg->hWin,
			                               WM_CF_SHOW, 0, ID_BUTTON_EXIT), "<");
			break;
		case WM_PAINT:
			GUI_SetBkColor((0xB0 << 24) | GUI_GRAY);
			GUI_Clear();
			break;
		case WM_NOTIFY_PARENT:
		{
			int NCode = pMsg->Data.v;
			int Id = WM_GetId(pMsg->hWinSrc);
			switch(Id)
			{
				case ID_LISTBOX_REPLY:
				{
					static uint8_t count = 0;
					switch(NCode)
					{
						case WM_NOTIFICATION_RELEASED:
						{
							count++;
							if(count == 2)
							{
								count = 0;
								char buf[32];
								LISTBOX_GetItemText(hList, LISTBOX_GetSel(hList), buf, sizeof(buf));
								//snprintf(buf, sizeof(buf),"%s Clicked",buf2);
								//GUI_MessageBox(buf, "Message", GUI_MESSAGEBOX_CF_MOVEABLE);
								//Create dialog
								CreateDispWindow(pMsg->hWin, buf);
							}
							break;
						}
						case WM_NOTIFICATION_SEL_CHANGED:
							count = 0;
							break;
					}
					break;
				}
				case ID_BUTTON_EXIT:
					if(NCode == WM_NOTIFICATION_RELEASED)
					{
						WM_DeleteWindow(pMsg->hWin);
						CreateMainWindow();
					}
					break;
			}
		}
		default:
			WM_DefaultProc(pMsg);
			break;
	}
}

void CreateReplyWindow()
{
	hRep = WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(),
	                       WM_CF_SHOW | WM_CF_HASTRANS | WM_CF_MEMDEV_ON_REDRAW, _cbRep, 0);
}
